const api = require('../../network/api')
import {
    delay
} from '../../utils/util'

Page({

    /**
     * 页面的初始数据
     */
    data: {
        categotys: [],
        active: 0,
        cartoons: [],
        topPosition: 0,
        toView: 'green',
        query: {
            page: 1,
            pageSize: 30,
            category_id: ''
        },
        loadingData: false, //避免用户瞬间多次下滑到底部，发生多次数据加载事件
        hidden: true //用户控制滑到底部出现数据正在加载中

    },
    onLoad: function(options) {

    },
    onShow: function() {
        this.getCategory()
        this.getCartoons()

    },
    async getCategory() {
        const res = await api.getCtcategory({
            isAll: true
        })
        console.log(res, 'res');
        if (res.code == 200) {
            res.data.data.unshift({
                id: 0,
                name: '全部'
            })
            this.setData({
                    categotys: res.data.data
                })
                // console.log(this.data.categotys, 'categotys');
        }
    },
    async getCartoons() {
        const res = await api.getCartoons(this.data.query)
        if (res.code == 200) {
            const list = this.data.cartoons
            list.push(...res.data.data)
            this.setData({
                cartoons: list,
                hidden: false
            })
        }
        // console.log(this.data.cartoons, 'cartoons');
    },
    // 切换分类
    tabData(e) {
        console.log(e.currentTarget.dataset.id, 'id')
        let category_id = e.currentTarget.dataset.id
        this.data.query.category_id = category_id
        if (category_id == 0) {
            this.data.query.category_id = ''
        }
        this.data.query.page = 1
        const query = this.data.query
        this.setData({
            active: e.currentTarget.dataset.active,
            query,
            cartoons: []
        })
        this.getCartoons()
    },
    async lower(e) {
        console.info('scrollToLower', e);
        let hidden = this.data.hidden,
            loadingData = this.data.loadingData;
        if (hidden) {
            this.setData({
                hidden: false
            });
            console.info(this.data.hidden);
        }
        if (loadingData) {
            return;
        }
        this.data.query.page += 1
        this.setData({
            loadingData: true
        });
        // 加载数据,模拟耗时操作
        // wx.showLoading({
        //   title: '数据加载中...',
        // });
        this.setData({
            hidden: true,
            loadingData: false
        });
        await delay(1000)
        this.getCartoons()

        // wx.hideLoading();
    },
    scroll(e) {
        // console.log(e,'scroll')
    },
    scrollToTop() {
        this.setAction({
            scrollTop: 0
        })
    },
    onDetail(e) {
        console.log(e.currentTarget.dataset.id);
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: `/pages/detail/index?id=${id}`,
        })

    }

})